package com.zhixing.order.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class ThreadPoolConfig {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor() {
        int cpuCores = Runtime.getRuntime().availableProcessors();
        int ioWaitRatio = 3;
        int corePoolSize = cpuCores * (1 + ioWaitRatio);
        return new ThreadPoolExecutor(
                corePoolSize,
                2 * corePoolSize,
                60,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(2 * corePoolSize),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );
    }
}
